package cn.ecnu.common.utils;

import cn.ecnu.mapper.recommend.RecommendCompanyMapper;
import cn.ecnu.mapper.resume.ResumeEduMapper;
import cn.ecnu.pojo.match.MatchForCandidate;
import cn.ecnu.pojo.match.MatchForRecommender;
import cn.ecnu.pojo.recommend.RecommendCompany;
import cn.ecnu.pojo.recommend.RecommendJob;
import cn.ecnu.pojo.recommend.RecommendRequire;
import cn.ecnu.pojo.resume.*;
import cn.ecnu.service.match.MatchCandidateService;
import cn.ecnu.service.match.MatchRecommenderService;
import cn.ecnu.service.recommend.RecommendCompanyService;
import cn.ecnu.service.recommend.RecommendJobService;
import cn.ecnu.service.resume.*;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.nlp.v20190408.NlpClient;
import com.tencentcloudapi.nlp.v20190408.models.*;
import org.springframework.stereotype.Component;


import javax.annotation.Resource;
import java.util.List;

@Component
public class TencentCloudUtil
{
    public static Similarity[] send(String src, String target) {
        TextSimilarityResponse response = null;
        try{
            // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
            Credential cred = new Credential("AKID1oLbaIjtnQmrfCRQ8ZkeHdiW3IGxNWO0", "PXOL9gvuc3D7j59wRgeLlvpSpvlJaZIk");
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("nlp.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            NlpClient client = new NlpClient(cred, "ap-guangzhou", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            TextSimilarityRequest req = new TextSimilarityRequest();
            req.setSrcText(src);

            String[] targetText1 = {target};
            req.setTargetText(targetText1);
            // 返回的resp是一个TextSimilarityResponse的实例，与请求对象对应
            response = client.TextSimilarity(req);
            // 输出json格式的字符串回包
            //System.out.println(TextSimilarityResponse.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
            return null;
        }
        return response.getSimilarity();
    }


    @Resource
    ResumeEduService resumeEduService;
    @Resource
    ResumeExtraService resumeExtraService;
    @Resource
    ResumeJobService resumeJobService;
    @Resource
    ResumeSkillService resumeSkillService;

    @Resource
    MatchCandidateService matchCandidateService;

    @Resource
    RecommendCompanyService recommendCompanyService;
    @Resource
    RecommendCompanyMapper recommendCompanyMapper;
    @Resource
    RecommendJobService recommendJobService;

    public void allMatchForCandidate(ResumeExp cur){
        Integer candidateId = cur.getUserId();
        List<Integer> IDs = recommendCompanyMapper.selectAllIds();
        for (Integer recommenderId : IDs) {
            if (recommenderId.equals(candidateId)) continue;
            RecommendCompany company = recommendCompanyService.getById(recommenderId);
            RecommendJob job = recommendJobService.getById(recommenderId);
            if (job == null) continue;

            MatchForCandidate matchCandidate = new MatchForCandidate();
            matchCandidate.setCandidateId(candidateId);
            matchCandidate.setRecommenderId(recommenderId);

            String src1 = "公司:" + cur.getExpCompany() + " 职位:" + cur.getExpPosition() + " 地点:" + cur.getExpJobLocation() + " 类型:" + cur.getExpJobType();
            String target1 = "公司:" + company.getRecCompany() + " 职位:" + job.getRecPosition() + " 地点:" + job.getRecJobLocation() + " 类型:" + job.getRecJobType();
            Similarity[] result1 = send(src1, target1);
            double index1 = (result1 != null ? result1[0].getScore() : 0);
            matchCandidate.setMatchCompanyPosLocType(index1);

            Double expSalary = Double.valueOf(cur.getExpSalary());
            Double recSalary = Double.valueOf(job.getRecSalary());
            double index2 = recSalary / expSalary;
            matchCandidate.setMatchSalary(index2);

            String src2 = cur.getExpDetail();
            String target2 = job.getRecDetail();
            double index3;
            if (src2 == null || target2 == null){
                index3 = 0.0;
                matchCandidate.setMatchJobDetail(0.0);
            }else{
                Similarity[] result2 = send(src2, target2);
                index3 = (result2 != null ? result2[0].getScore() : 0);
                matchCandidate.setMatchJobDetail(index3);
            }

            matchCandidate.setSynthesis(index1 + index2/10 + index3);

            MatchForCandidate one = matchCandidateService.getOne(Wrappers.<MatchForCandidate>lambdaQuery()
                    .eq(MatchForCandidate::getCandidateId, candidateId)
                    .eq(MatchForCandidate::getRecommenderId, recommenderId));
            if (one != null){
                matchCandidateService.update(matchCandidate, Wrappers.<MatchForCandidate>lambdaUpdate()
                    .eq(MatchForCandidate::getCandidateId, candidateId)
                    .eq(MatchForCandidate::getRecommenderId, recommenderId));
            }else {
                matchCandidateService.save(matchCandidate);
            }
        }
        System.out.println("okk");
    }


    @Resource
    ResumeEduMapper resumeEduMapper;
    @Resource
    MatchRecommenderService matchRecommenderService;

    public void allMatchForRecommender(RecommendRequire recommendRequire){
        Integer recommenderId = recommendRequire.getUserId();
        String require = recommendRequire.getPositionRequire();
        List<Integer> IDs = resumeEduMapper.selectAllIds();
        for (Integer candidateId : IDs) {
            if (candidateId.equals(recommenderId)) continue;
            List<ResumeEdu> resumeEdus = resumeEduService.list(Wrappers.<ResumeEdu>lambdaQuery().eq(ResumeEdu::getUserId, candidateId));
            List<ResumeExtra> resumeExtras = resumeExtraService.list(Wrappers.<ResumeExtra>lambdaQuery().eq(ResumeExtra::getUserId, candidateId));
            List<ResumeSkill> resumeSkills = resumeSkillService.list(Wrappers.<ResumeSkill>lambdaQuery().eq(ResumeSkill::getUserId, candidateId));
            List<ResumeJob> resumeJobs = resumeJobService.list(Wrappers.<ResumeJob>lambdaQuery().eq(ResumeJob::getUserId, candidateId));
            if (resumeExtras == null || resumeSkills == null || resumeJobs == null) {
                continue;
            }
            String src = require;
            String target1 = resumeEdus.toString();
            String target2 = resumeExtras.toString();
            String target3 = resumeJobs.toString();
            String target4 = resumeSkills.toString();

            Similarity[] result1 = send(src, target1); Double index1 = (double) (result1 != null ? result1[0].getScore() : 0);
            Similarity[] result2 = send(src, target2); Double index2 = (double) (result2 != null ? result2[0].getScore() : 0);
            Similarity[] result3 = send(src, target3);Double  index3 = (double) (result3 != null ? result3[0].getScore() : 0);
            Similarity[] result4 = send(src, target4);Double  index4 = (double) (result4 != null ? result4[0].getScore() : 0);

            MatchForRecommender matchRecommender = new MatchForRecommender();
            matchRecommender.setCandidateId(candidateId);
            matchRecommender.setRecommenderId(recommendRequire.getUserId());

            matchRecommender.setMatchEdu(index1);
            matchRecommender.setMatchExtra(index2);
            matchRecommender.setMatchJob(index3);
            matchRecommender.setMatchSkill(index4);

            matchRecommender.setSynthesis(index1+index2+index3+index4);

            MatchForRecommender one = matchRecommenderService.getOne(Wrappers.<MatchForRecommender>lambdaQuery()
                    .eq(MatchForRecommender::getCandidateId, candidateId)
                    .eq(MatchForRecommender::getRecommenderId, recommendRequire.getUserId()));
            if (one != null){
                matchRecommenderService.update(matchRecommender, Wrappers.<MatchForRecommender>lambdaUpdate()
                        .eq(MatchForRecommender::getCandidateId, candidateId)
                        .eq(MatchForRecommender::getRecommenderId, recommendRequire.getUserId()));
            }else {
                matchRecommenderService.save(matchRecommender);
            }
        }
    }

}